<template>
    <div class="listDiv pd10">
        <div class="searchDiv">
            <el-form class="searchLeft" ref="serform" :model="obj.req" label-position="top">
                <el-form-item class="serlabelB mr20" label="配置类别：" prop="type">
                    <el-select v-model="obj.req.type" placeholder="请选择配置类别" style="width: 100px;">
                        <el-option v-for="(item,index) in proxy.$setting.globalData.configAry" :key="index" :label="item.text" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item class="serlabelB mr20" label="配置名称：" prop="key">
                    <el-input v-model="obj.req.key" placeholder="请输入配置名称" clearable />
                </el-form-item>
                <el-form-item class="mr20">
                    <el-button type="primary" @click="obj.req.currentpage=1;init()">查询</el-button>
                    <el-button plain @click="proxy.$refs['serform'].resetFields();init()">重置</el-button>
                </el-form-item>
            </el-form>
            <el-button type="primary" @click="add">添加配置</el-button>
        </div>
        <el-table :data="obj.ary" style="width: 100%;flex: 1;"
            :header-cell-style="{ background: '#E5E6EB', color: '#333333' }">
            <!-- <el-table-column prop="id" label="ID"></el-table-column> -->
            <el-table-column prop="name" label="配置名称"></el-table-column>
            <el-table-column prop="type" label="类别" width="140">
                <template #default="scope">
                    <span>{{ proxy.$util.aryVal("configAry", scope.row.type) }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="name" label="状态" width="140">
                <template #default="scope">
                    <el-switch v-model="scope.row.isshow" size="small" :validate-event="false" @change="setstate(scope.row)"
                        active-text="正常" inactive-text="停用"
                        :active-value="1" :inactive-value="0">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column prop="height" label="操作" width="90">
                <template #default="scope">
                    <el-button type="primary" size="small"@click="edit(scope.row)">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-drawer v-model="obj.drawer.show" :title="obj.drawer.title" size="50%" direction="rtl" :close-on-click-modal="false" destroy-on-close>
            <Edit v-model:data="obj.drawer.data" @init="init" @close="obj.drawer.show = false" />
        </el-drawer>
    </div>
</template>

<script setup>
    import { reactive,getCurrentInstance } from 'vue'
    import Edit from './edit.vue'
    let { proxy } = getCurrentInstance()

    let obj = reactive({
        drawer: {
            show: false,
            title: "",
            data: {}
        },
        ary:[],
        req: {
            key:"",
            type: 0
        }
    })

    let init = () => {
        proxy.$ajax({
            url: '/manager/config/list',
            data: obj.req,
            bfLoading: true
        }).then(d => {
            // console.log(d)
            if(d.success){
                obj.ary = d.msg.list
            }
        }).catch(() => { })
    }
    init()

    let add = () => {
        obj.drawer.show = true
        obj.drawer.title = `添加配置(${proxy.$util.aryVal("configAry", obj.req.type)})`
        obj.drawer.data = { id:0,type:obj.req.type, name:"" }
    }
    let edit = x => {
        obj.drawer.show = true
        obj.drawer.title = `修改配置(${proxy.$util.aryVal("configAry", x.type)})`
        obj.drawer.data = x;
    }
    let setstate = x => {
        proxy.$ajax({
            url: '/manager/config/setstate',
            data: { id: x.id, type: x.type },
            bfLoading: true
        }).then(da => {
            if(da.success){
                proxy.elMessageMixin(`配置状态变更成功`, 'success')
                init()
            }else{
                proxy.elMessageMixin(d.err, 'error')
            }
        }).catch(() => { })
    }
</script>

<style scoped>
</style>